﻿// -----------------------------------------------------------------------
// <copyright file="IMG_BinaryImage.cs" company="Hewlett-Packard">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace ImageProcessing.ImageProcessor
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public static class IMG_BinaryImage
    {
        /// <summary>
        /// Converts to binary.
        /// </summary>
        /// <param name="image">The image.</param>
        /// <param name="threshold">The threshold.</param>
        /// <returns>binary image</returns>
        public static int[,] ConvertToBinary(Entities.IMG_Image image, int threshold)
        {
            try
            {
                int[,] returnedImage = new int[image.Height, image.Width];
                double temp;
                for (int i = 0; i < image.Height; i++)
                {
                    for (int j = 0; j < image.Width; j++)
                    {
                        temp = image[i, j].Red + image[i, j].Green + image[i, j].Blue;
                        temp /= 3;
                        returnedImage[i, j] = temp >= threshold ? 1 : 0;
                    }
                }

                return returnedImage;
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                throw;
            }
        }

        /// <summary>
        /// Converts to binary.
        /// </summary>
        /// <param name="image">The image.</param>
        /// <param name="threshold">The threshold.</param>
        /// <returns>Binary Image</returns>
        public static Entities.IMG_Image ConvertToBinaryImage(Entities.IMG_Image image, int threshold)
        {
            try
            {
                Entities.IMG_Image retImage = new Entities.IMG_Image(image.Width, image.Height);
                double temp;
                for (int i = 0; i < image.Height; i++)
                {
                    for (int j = 0; j < image.Width; j++)
                    {
                        temp = image[i, j].Red + image[i, j].Green + image[i, j].Blue;
                        temp /= 3;
                        retImage[i, j] = temp >= threshold ? new Entities.Pixel(255, 255, 255) : new Entities.Pixel(0, 0, 0);
                    }
                }

                return retImage;
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                throw;
            }
        }
    }
}
